package com.pe.pgn.clubpgn.webapp.controller.stockreserva;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.SessionStatus;

import com.pe.pgn.clubpgn.common.CLPConstantes;
import com.pe.pgn.clubpgn.domain.ClpbEstacion;
import com.pe.pgn.clubpgn.domain.beans.BNDocumentoMovArticulo;
import com.pe.pgn.clubpgn.domain.beans.BNStockReserva;
import com.pe.pgn.clubpgn.model.User;
import com.pe.pgn.clubpgn.service.StockReservaService;
import com.pe.pgn.clubpgn.webapp.controller.BaseFormController;

/**
 * @author frank.ayala
 * */

@Controller
@RequestMapping("**/stockreserva/*")
public class StockReservaController extends BaseFormController{

	@Autowired
	private StockReservaService stockReservaService;
	
	@RequestMapping(value = "/stockingresolist.html")
    public @ModelAttribute("reservaform") BNStockReserva listarReservasNoIngresadas(
    	@ModelAttribute("reservaform") BNStockReserva bndocumento, Model model) {

		String coEstacionUsuario = "-1";
		User beanUser = stockReservaService.getUsuarioLogueado();
		ClpbEstacion estacion = beanUser.getClpbEstacion();
		
		if(estacion != null){
			coEstacionUsuario = estacion.getId().toString();
		}
		
		List<BNDocumentoMovArticulo> reservas  = stockReservaService.busquedaReserva(coEstacionUsuario,
		CLPConstantes.ST_VERDADERO,	CLPConstantes.ST_DOCUMENTO_RESERVA, CLPConstantes.NO_INGRESADO);

		bndocumento = new BNStockReserva();
		bndocumento.setDocumentoDetalle(reservas);
		model.addAttribute("reservaform" , bndocumento);
		return bndocumento;
    }
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/stockingresoform.html", method = RequestMethod.POST)
    public String guadarIngresoStockReserva(
    	@ModelAttribute("reservaform") BNStockReserva bndocumento,
    	BindingResult result, SessionStatus status, HttpServletRequest request, Model model) {

		Map mapMessage = stockReservaService.guardarStockIngresado
		(bndocumento.getDocumentoDetalle());
		
		if("success".equalsIgnoreCase(String.valueOf(mapMessage.get("msg")))){
			saveMessage(request, getText("common.added", request.getLocale()));
		}else if("error".equalsIgnoreCase(String.valueOf(mapMessage.get("msg")))){
			saveError(request, mapMessage.get("dtl").toString());
		}else{
			saveError(request, getText("errors.general", request.getLocale()));
		}
		
		return "redirect:stockingresolist.html?from=list";
    }
	
	@RequestMapping(value = "/stockanulacionlist.html")
    public @ModelAttribute("reservaform") BNStockReserva listarReservasIngresadas(
    	@ModelAttribute("reservaform") BNStockReserva bndocumento, Model model) {

		String coEstacionUsuario = "-1";
		User beanUser = stockReservaService.getUsuarioLogueado();
		ClpbEstacion estacion = beanUser.getClpbEstacion();
		
		if(estacion != null){
			coEstacionUsuario = estacion.getId().toString();
		}
		
		List<BNDocumentoMovArticulo> reservas  = stockReservaService.busquedaReserva(coEstacionUsuario,
		CLPConstantes.ST_VERDADERO,	CLPConstantes.ST_DOCUMENTO_RESERVA, CLPConstantes.INGRESADO);

		bndocumento = new BNStockReserva();
		bndocumento.setDocumentoDetalle(reservas);
		model.addAttribute("reservaform" , bndocumento);
		return bndocumento;
    }
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/stocknoingresoform.html", method = RequestMethod.POST)
    public String guadarNoIngresoStockReserva(
    	@ModelAttribute("reservaform") BNStockReserva bndocumento,
    	BindingResult result, SessionStatus status, HttpServletRequest request, Model model) {

		Map mapMessage = stockReservaService.guardarStockNoIngresado
		(bndocumento.getDocumentoDetalle());
		
		if("success".equalsIgnoreCase(String.valueOf(mapMessage.get("msg")))){
			saveMessage(request, getText("common.added", request.getLocale()));
		}else if("error".equalsIgnoreCase(String.valueOf(mapMessage.get("msg")))){
			saveError(request, mapMessage.get("dtl").toString());
		}else{
			saveError(request, getText("errors.general", request.getLocale()));
		}
		
		return "redirect:stockanulacionlist.html?from=list";
    }
}
